package com.lai.note.controller.user;

import com.lai.note.dto.MessageBean;
import com.lai.note.model.User;
import com.lai.note.service.UserNoteService;
import com.lai.note.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/user/note")
@Validated
public class UserNoteAction {

    @Autowired
    private UserNoteService userNoteService;

    //新建笔记
    @RequestMapping(value = "/newNote.action",method = RequestMethod.POST)
    public MessageBean createNewNote(HttpSession session,@NotNull(message = "标题不能为空") String title,@NotNull(message = "笔记内容不能为空") String content,String lableId){
        System.out.println("session:-->"+session);
        System.out.println("content:-->"+content+"  lableId:-->"+lableId);
        User user = (User)session.getAttribute("user");
        System.out.println("新增笔记的用户为：-->"+user);
        if(userNoteService.createNewNote(user.getUserId(),title,content,lableId))
            return MessageUtil.success();
        return MessageUtil.failure();
    }

    //搜索笔记
    @RequestMapping(value = "/searchNoteByTime.action",method = RequestMethod.GET)
    public MessageBean searchNoteByTime(@NotNull(message = "请按时间查询") String time){
        System.out.println("搜索的time为："+time);
        return MessageUtil.success(userNoteService.searchNoteByTime(time));
    }


    //修改笔记 注：修改代码
    // TODO: 2021/6/28 要修改传过来的参数 noteId title content ==>Note note
    @RequestMapping(value = "/updateNote.action",method = RequestMethod.POST)
    public MessageBean updateNote(@NotNull String noteId,@NotNull(message = "笔记标题不能为空") String title,@NotNull(message = "新内容不能为空") String newContent){
        if(userNoteService.updateNote(noteId,title,newContent))
            return MessageUtil.success();
        return MessageUtil.failure();
    }

    //删除笔记
    @RequestMapping(value = "/deleteNote.action",method = RequestMethod.DELETE)
    public MessageBean deleteNote(String noteId){
        if(userNoteService.deleteNote(noteId))
            return MessageUtil.success();
        return MessageUtil.failure();
    }

    /*
    * 用户查看自己笔记详情
    * @return List<Note>
    * */
    @RequestMapping(value = "/lookNoteDetails.action",method = RequestMethod.GET)
    public MessageBean userLookNoteDetails(HttpSession session){
        User user = (User) session.getAttribute("user");
        return MessageUtil.success(userNoteService.selectUserAllNotes(user.getUserId()));
    }




}